package com.project.es.controller;

import com.project.es.entity.UserES;
import com.project.es.repository.UserESRepository;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserESRepository repositoryEs;

    private String names;

    @GetMapping("/create")
    public String create(
            @RequestParam("id") Long id,
            @RequestParam("userName") String userName,
            @RequestParam("userPhone") String userPhone) {
        UserES userES = new UserES();
        userES.setId(id);
        userES.setUserName(userName);
        userES.setUserPhone(userPhone);
        return repositoryEs.save(userES).toString();
    }

    @GetMapping("/get")
    public String get() {
        names = "";
        Iterable<UserES> userES = repositoryEs.findAll();
        userES.forEach(userES1 -> {
            names += userES1.toString() + "\n";
        });
        return names;
    }

    private String searchs = "";

//    @GetMapping("/search")
//    public String search(@RequestParam("searchKey") String searchKey) {
//        searchs = "";
//        // 构建查询条件
//        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//        // 添加基本分词查询
//        queryBuilder.withQuery(QueryBuilders.matchQuery("userName", searchKey));
//        // 搜索，获取结果
//        Page<UserES> items = repositoryEs.search(queryBuilder.build());
//        // 总条数
//        long total = items.getTotalElements();
//        searchs += "总共数据数：" + total + "\n";
//        items.forEach(userES -> searchs += userES.toString() + "\n");
//        return searchs;
//    }


}
